//
//  RegisterView.m
//  DingDongXiChe
//
//  Created by xieyingze on 2017/12/31.
//  Copyright © 2017年 xieyingze. All rights reserved.
//

#import "RegisterView.h"


@interface RegisterView ()

@property (nonatomic, strong) UIView *mainView;

@property (nonatomic, strong) BaseTextField *phoneTextField;

@property (nonatomic, strong) BaseTextField *passwordField;

@property (nonatomic, strong) BaseNextButton *nextButton;

@end

@implementation RegisterView

#pragma mark - init

- (void)initSubViews {
    [self addSubview:self.mainView];
    [self addSubview:self.nextButton];
}

- (void)autoLayoutSubViews {
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0).insets(UIEdgeInsetsMake(5, 20, 100, 20));
    }];
    [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainView.mas_bottom).offset(40);
        make.centerX.equalTo(@0);
        make.width.equalTo(@(200 * kUI_Percent));
        make.height.equalTo(@54);
    }];
}

- (void)bindViewModel {
    @weakify(self);
    //合并信号
    RACSignal *availabelSingal = [RACSignal combineLatest:@[self.phoneTextField.rac_textSignal, self.passwordField.rac_textSignal] reduce:^id _Nullable(NSString *str1, NSString *str2) {
        @strongify(self);
        str1 = [str1 stringByReplacingOccurrencesOfString:@" " withString:@""];
        self.viewModel.registerPhone = str1;
        self.viewModel.registerPSW = str2;
        return (str1.length == 11 && str2.length > 0) ?  @(YES) : @(NO);
    }];
    [availabelSingal subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        self.nextButton.available = [x boolValue];
        [self.viewModel.registerButtonAvailableSubject sendNext:x];
    }];
    
    [[self.nextButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self.viewModel.gotoRegisterSubject sendNext:x];
    }];
}

#pragma mark - method


#pragma makr - Event


#pragma mark - Setter


#pragma mark - Getter

- (UIView *)mainView
{
    if (!_mainView) {
        _mainView = [UIView new];
        _mainView.backgroundColor = kCOLOR_white;
        _mainView.layer.cornerRadius = kUI_LayerCorner;
        [_mainView setViewShadow:kCOLOR_shadowLight offset:CGSizeMake(0, 0) radius:kUI_ShawRadius];
        
        //添加手机标签
        BaseLabel *phoneLabel = [BaseLabel new];
        [phoneLabel setLabelStyle:@"手机" textColor:KCOLOR_font_light textFont:kFONT_mainFont(12) texrAlignment:NSTextAlignmentLeft];
        [_mainView addSubview:phoneLabel];
        [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(@20);
            make.height.equalTo(@20);
        }];
        
        BaseTextField *phoneTextField = [BaseTextField new];
        phoneTextField.isNeedSpacing = NO;
        phoneTextField.formatType = FormatTypePhone;
        [phoneTextField setTextFieldPlaceHolder:@"" textFont:kFONT_HelBold(12) textColor:KCOLOR_font_special textAlignment:NSTextAlignmentLeft];
        self.phoneTextField = phoneTextField;
        [_mainView addSubview:phoneTextField];
        [phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(phoneLabel.mas_bottom).offset(10);
            make.left.equalTo(@20);
            make.right.equalTo(@-20);
            make.height.equalTo(@20);
        }];
        
        UIView *line1 = [UIView new];
        line1.backgroundColor = kCOLOR_lightLine;
        [_mainView addSubview:line1];
        [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(phoneTextField.mas_bottom).offset(5);
            make.left.right.equalTo(phoneTextField);
            make.height.equalTo(@0.5);
        }];
        
        //添加密码标签
        BaseLabel *passwordLabel = [BaseLabel new];
        [passwordLabel setLabelStyle:@"密码" textColor:KCOLOR_font_light textFont:kFONT_mainFont(12) texrAlignment:NSTextAlignmentLeft];
        [_mainView addSubview:passwordLabel];
        [passwordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(line1.mas_bottom).offset(20);
            make.left.equalTo(phoneLabel);
            make.height.equalTo(phoneLabel);
        }];
        
        BaseTextField *passwordField = [BaseTextField new];
        passwordField.isNeedSpacing = NO;
        passwordField.formatType = FormatTypePassword;
        passwordField.secureTextEntry = YES;
        [passwordField setTextFieldPlaceHolder:@"" textFont:kFONT_HelBold(12) textColor:KCOLOR_font_special textAlignment:NSTextAlignmentLeft];
        self.passwordField = passwordField;
        [_mainView addSubview:passwordField];
        [passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(passwordLabel.mas_bottom).offset(10);
            make.left.equalTo(phoneTextField);
            make.right.equalTo(phoneTextField);
            make.height.equalTo(phoneTextField);
        }];
        
        UIView *line2 = [UIView new];
        line2.backgroundColor = kCOLOR_lightLine;
        [_mainView addSubview:line2];
        [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(passwordField.mas_bottom).offset(5);
            make.left.right.equalTo(passwordField);
            make.height.equalTo(@0.5);
        }];
    }
    return _mainView;
}

- (BaseNextButton *)nextButton {
    if (!_nextButton) {
        _nextButton = [BaseNextButton new];
        [_nextButton setButtonStyle:@"注册" titleColor:kCOLOR_white titleFont:kFONT_HelBold(16) texrAlignment:NSTextAlignmentCenter];
        _nextButton.layer.cornerRadius = 54.0/2;
        [_nextButton setViewShadow:KCOLOR_mainColor offset:CGSizeMake(0, 0) radius:kUI_ShawRadius/2];
        _nextButton.available = NO;
    }
    return _nextButton;
}

- (DDRegAndLogViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [DDRegAndLogViewModel new];
    }
    return _viewModel;
}

@end
